package com.thd.springboottest.kafka.controller;

import com.thd.springboottest.kafka.service.KafkaProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
 * com.thd.springboottest.kafka.controller.KafkaController
 *
 * @author: wanglei62
 * @DATE: 2022/3/29 11:52
 **/
@Controller
@RequestMapping("/kafka")
public class KafkaController {

    @Autowired
    private KafkaProducerService producerService;
    // 发送消息
    @GetMapping("/sendMessageAsync/{message}")
    @ResponseBody
    // url http://127.0.0.1:8899/thd/kafka/sendMessageAsync/xxx
    public String sendMessageAsync(@PathVariable("message") String message)  throws InterruptedException, ExecutionException, TimeoutException {
        producerService.sendMessageAsync("test",message + "_" + new Date().getTime());
        return "SUCCESS";
    }

    // 发送消息
    @GetMapping("/sendMessageSync/{message}")
    @ResponseBody
    public String sendMessageSync(@PathVariable("message") String message) throws InterruptedException, ExecutionException, TimeoutException {
        producerService.sendMessageSync("test",message + "_" + new Date().getTime());
        return "SUCCESS";
    }

}
